﻿@typeparam T where T : Enum
@inherits CfgBase<JsVal?>
<div class="cfg">
    <div class="over-input">
        <Switch @bind-Value="IsShow" DisplayText="@DisplayText" ShowLabel="true" ShowInnerText="true"
                OnValueChanged="ShowChanged"
                OffInnerText="详情" OnInnerText="详情"></Switch>

        @if (IsShow && ValueHander != null && JsTypes.Count>1)
        {
            <Select @bind-Value="ValueHander.JsType" Items="JsTypesItems" OnSelectedItemChanged="SelectedTypeChanged"> </Select>
        }
        <PopConfirmButton Size="Size.ExtraSmall" Placement="Placement.Top"
                          Color="@IsDefect()"
                          ConfirmIcon="fa-solid fa-triangle-exclamation text-danger"
                          OnConfirm="DefaultValue"
                          Text="默认"
                          Content="确认恢复默认值?" />
        <Tooltip Title="@HelpInfo" CustomClass="is-invalid">
            <i style="color:@IsOk()" class="fas fa-circle-question" />
        </Tooltip>
    </div>
    @if (IsShow && ValueHander != null)
    {
        switch (ValueHander.JsType)
        {
            case JsType.Enum:
                <Select Value="@ValueHander.RAW" Items="EnumItems" OnSelectedItemChanged="OnSelectedItemChangedForEnum"></Select>
                break;
            case JsType.EnumArray:
                <MultiSelect @bind-Value="@SelectedEnumValues" OnSelectedItemsChanged="OnValueChangedForMEnum" />
                break;
            case JsType.String:
                <BootstrapInput Value="@ValueHander.RAW" OnValueChanged="OnValueChangedForRaw"></BootstrapInput>
                break;
            case JsType.Bool:
                <Checkbox Value="@ValueHander.GetBool()" OnValueChanged="OnValueChangedForRawBool"></Checkbox>
                break;
            case JsType.Array:
                <Textarea Value="@ValueHander.RAW" placeholder="[]" OnValueChanged="OnValueChangedForRaw"></Textarea>
                break;
            case JsType.Object:
                <Textarea Value="@ValueHander.RAW" placeholder="{}" OnValueChanged="OnValueChangedForRaw"></Textarea>
                break;
            case JsType.Num:
                <BootstrapInputNumber Value="@ValueHander.GetNum()" OnValueChanged="OnValueChangedForRawNum"></BootstrapInputNumber>
                break;
            case JsType.Function:
                <Textarea Value="@ValueHander.RAW" OnValueChanged="OnValueChangedForRaw" placeholder=" function(value) {return value.min - 20;}" rows="4" />
                break;
            default:
                <BootstrapInput Value="@ValueHander.RAW" OnValueChanged="OnValueChangedForRaw"></BootstrapInput>
                break;
        }
    }
</div>
@code {
    /// <summary>
    /// 为多枚举的时候,选择了哪些,由于T是Enum,所以会自动映射Items
    /// </summary>
    public List<T> SelectedEnumValues { get; set; } = new();
    /// <summary>
    /// 很重要,不能为空
    /// </summary>
    [Parameter, EditorRequired]
    public List<JsType> JsTypes { get; set; } = new();
    /// <summary>
    /// 对应JsTypes
    /// </summary>
    public List<SelectedItem> JsTypesItems { get; set; } = new List<SelectedItem>();
    /// <summary>
    /// 选择为枚举或者多枚举的时候
    /// </summary>
    public List<SelectedItem> EnumItems { get; set; } = new List<SelectedItem>();
    private SelectedItem SelectedItem = new SelectedItem();


    public string IsOk()
    {
        return ValueHander == null ? "green" : ValueHander.IsOk() ? "green" : "black";
    }
    public Color IsDefect()
    {
        var ss = ValueHander;
        return ValueHander == null ? Color.Secondary : Color.Success ;
    }
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        JsTypesItems = JsTypes.Select(x => new SelectedItem(x.ToString(), x.GetDescription())).ToList();
        if (typeof(T).IsEnum)
        {

            foreach (var value in Enum.GetValues(typeof(T)))
            {
                //GetDescription
                if (value != null)
                {
                    EnumItems.Add(new SelectedItem(value.ToString()!, value.GetDescription()));
                }
            }

        }
        if (ValueHander != null)
        {   //未实现,因为不好反序列化回来
            var sss = ValueHander.RAW.Replace("[", "").Replace("]", "").Replace("\\", "").Replace("\"", "").Replace(" ", "");
            string[] stringValues = sss.Split(',');

            var enumType = typeof(T);
            foreach (var stringValue in stringValues)
            {
                if (Enum.TryParse(enumType, stringValue, out object? enumValue))
                {
                    SelectedEnumValues.Add((T)enumValue);
                }
            }
        }
        // EnumItems = JsTypes.Select(x => new SelectedItem(x.ToString(), x.GetDescription())).ToList();
    }
    /// <summary>
    /// 是否展示详情,如果是默认值,就初始化一下
    /// </summary>
    /// <param name="val"></param>
    /// <returns></returns>
    async Task ShowChanged(bool val)
    {
        if (ValueHander == null)
        {
            var t = JsTypes.First();
            ValueHander = new JsVal(t);
        }
        await Task.CompletedTask;
    }
    /// <summary>
    ///
    /// </summary>
    /// <param name="val"></param>
    /// <returns></returns>
    async Task OnValueChangedForRaw(string val)
    {
        if (ValueHander != null)
        {
            //todu  应该要判断值是否合法
            var obj = new JsVal(val, Value.JsType);
            if (obj.IsOk())
            {
                //ValueHander = obj;
                ValueHander.RAW = val;
                IsSucessed = "Green";
                Update();
            }
            else
            {
                IsSucessed = "black";
                await Msg.Warning("失败", "输入的值不正确,请检查");
            }
        }

        await Task.CompletedTask;
    }
    async Task OnValueChangedForRawNum(decimal val)
    {
        if (ValueHander != null)
        {
            //todu  应该要判断值是否合法
            var obj = new JsVal(val.ToString(), Value.JsType);
            if (obj.IsOk())
            {
                ValueHander.RAW = val.ToString();
                IsSucessed = "Green";
                Update();
            }
            else
            {
                IsSucessed = "black";
                await Msg.Warning("失败", "输入的值不正确,请检查");
            }
        }

        await Task.CompletedTask;
    }
    async Task OnValueChangedForRawBool(bool val)
    {
        if (ValueHander != null)
        {
            //todu  应该要判断值是否合法
            var obj = new JsVal(val.ToString(), Value.JsType);
            if (obj.IsOk())
            {
                ValueHander.RAW = val.ToString();
                IsSucessed = "Green";
                Update();
            }
            else
            {
                IsSucessed = "black";
                await Msg.Warning("失败", "输入的值不正确,请检查");
            }
        }

        await Task.CompletedTask;
    }
    /// <summary>
    /// 当改变数据类型的时候,重新初始化初始值
    /// </summary>
    /// <param name="val"></param>
    /// <returns></returns>
    async Task SelectedTypeChanged(SelectedItem val)
    {
        //判断当值的值是不是合法的
        if (ValueHander != null)//一定会进来
        {
            switch (ValueHander.JsType)
            {
                case JsType.Enum:
                    var res = Enum.GetValues(typeof(T)).GetValue(0);
                    if (res != null)
                    {
                        ValueHander.RAW = res.ToString();
                    }
                    else
                    {
                        ValueHander.RAW = "";
                    }
                    break;
                case JsType.EnumArray:
                    ValueHander.RAW = "true";
                    break;
                case JsType.Num:
                    ValueHander.RAW = "0";
                    break;
                case JsType.Bool:
                    ValueHander.RAW = "true";
                    break;
                case JsType.String:
                    ValueHander.RAW = "";
                    break;
                case JsType.Object:
                    ValueHander.RAW = "{}";
                    break;
                case JsType.Array:
                    ValueHander.RAW = "[]";
                    break;
                case JsType.Function:
                    ValueHander.RAW = "function(value) {return value.min - 20;}";
                    break;
            }
            IsSucessed = "green";
        }

        await Task.CompletedTask;
    }
    /// <summary>
    /// 数据类型为枚举的时候
    /// </summary>
    /// <param name="val"></param>
    /// <returns></returns>
    async Task OnSelectedItemChangedForEnum(SelectedItem val)
    {
        //判断当值的值是不是合法的
        if (ValueHander != null)//一定会进来
        {
            ValueHander.RAW = val.Value;
        }
        await Task.CompletedTask;
    }
    async Task OnValueChangedForMEnum(IEnumerable<SelectedItem> items)
    {
        if (ValueHander != null)
        {
            ValueHander.RAW = JsonSerializer.Serialize(items.Select(x => x.Value));
            Update();
        }
        await Task.CompletedTask;
    }
}
